package com.example.leo.mycharmingstudent.a0925;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.a0905.ToastUtil;

public class ServiceActivity extends AppCompatActivity {

    private static final String TAG = "ServiceActivity";

    //开启服务按钮
    private Button btnStartService;
    //停止服务按钮
    private Button btnStopService;
    //绑定服务
    private Button btnBindService;
    //解绑服务
    private Button btnUnbindService;
    //获取Service中的进度
    private Button btnProgress;

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        /**
         * service和activity建立了联系
         * @param name
         * @param service Service中onBind生命周期方法传来的
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(TAG, "onServiceConnected: ");
            mBinder = (MyService.MyBinder) service;
            Log.e(TAG, "onServiceConnected: " + mBinder.getProcess());
        }

        /**
         * service和activity失去了联系
         * onServiceDisconnected什么时候会调用？
         * @param name
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBinder = null;
        }
    };
    private MyService.MyBinder mBinder;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);
        findViews();
        initData();
        initEvent();
    }

    private void findViews() {
        btnStartService = findViewById(R.id.btn_start_service_activity_service);
        btnStopService = findViewById(R.id.btn_stop_service_activity_service);

        btnBindService = findViewById(R.id.btn_bind_service_activity_service);
        btnUnbindService = findViewById(R.id.btn_unbind_service_activity_service);

        btnProgress = findViewById(R.id.btn_progress_activity_service);
    }

    private void initData() {

    }

    private void initEvent() {
        btnStartService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //开启服务操作
                /*
                Service只会存在一个实例，多次启动不会创建多个实例
                Activity默认启动模式，每次启动都会创建一个Activity实例
                 */
                Intent intent = new Intent(ServiceActivity.this, MyService.class);
                startService(intent);
            }
        });
        btnStopService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //停止服务操作
                Intent intent = new Intent(ServiceActivity.this, MyService.class);
                stopService(intent);
            }
        });

        btnBindService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //绑定服务
                Intent intent = new Intent(ServiceActivity.this, MyService.class);
                bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
            }
        });
        btnUnbindService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //解绑服务
                unbindService(mServiceConnection);
            }
        });
        btnProgress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取service中的进度
                if (mBinder != null) {
                    ToastUtil.showToast(ServiceActivity.this,
                            String.valueOf(mBinder.getProcess()));
                }else {
                    ToastUtil.showToast(ServiceActivity.this,
                            "心里没点数么，binder是null");
                }

            }
        });
    }
}
